package com.linkage.jz.bankruptcy.activity.Project;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.linkage.jz.bankruptcy.BankruptcyApp;
import com.linkage.jz.bankruptcy.Consts;
import com.linkage.jz.bankruptcy.R;
import com.linkage.jz.bankruptcy.activity.RegistLogin.GetPwdOneActivity;
import com.linkage.jz.bankruptcy.activity.RegistLogin.PhoneLoginActivity;
import com.linkage.jz.bankruptcy.base.BaseActivity;
import com.linkage.jz.bankruptcy.bean.LoginInfo;
import com.linkage.jz.bankruptcy.bean.MyProInfo;
import com.linkage.jz.bankruptcy.bean.TokenInfo;
import com.linkage.jz.bankruptcy.utils.SPUtils;
import com.linkage.jz.bankruptcy.utils.SignUtil;
import com.linkage.jz.bankruptcy.widget.PublicNoticeDialog;
import com.linkage.jz.bankruptcy.widget.ShapeLoadingDialog;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONArray;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.tencent.qq.QQ;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.Headers;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;


/**
 * A login screen that offers login via email/password.
 */
public  class CreateProjectDetailTwoActivity extends BaseActivity implements OnClickListener{
    private static final String TAG = CreateProjectDetailTwoActivity.class.getSimpleName();


    private ShapeLoadingDialog shapeLoadingDialog ;
    @Override
    public void initData() {

    }
    private int num =1;
    private int tmpnum = 1;
    private ArrayList<View> editViews = new ArrayList<View>();

    @Override
    public void setListener() {
//        btn_login.setOnClickListener(this);
        top_back.setOnClickListener(this);
//        ll_phonelogin.setOnClickListener(this);
//        getPwd.setOnClickListener(this);
        top_mune1.setOnClickListener(this);
        top_mune2.setOnClickListener(this);
        im_groupadd.setOnClickListener(this);
//        im_groupadd2.setOnClickListener(this);
    }

//    EditText g_managersadminphone,g_managersadminphone2,g_managersadmin,g_managersadmin2,g_managersname2,g_managersname1,p_managersname1,p_managersname2;
//    EditText socialUniformCreditCode,socialUniformCreditCode2,contacts,contacts2,contactsphone,contactsphone2;
    @Override
    public void initTitle() {
        top_title = findViewById(R.id.top_title);
        if(m_addtype == 1) {
            top_title.setText("新建项目");
        }else{
            top_title.setText("编辑项目");
        }
//        top_mune.setVisibility(View.INVISIBLE);
    }

    @Override
    public void initView() {

    }

    ImageView mLeftLogo;
    ImageView im_groupadd;//,im_groupadd2;
    EditText et_username,et_courtno;
    TextView et_courtname;
    EditText et_password,contacts,contactsphone;
    Button btn_login;
    TextView top_title,getPwd;
    Button top_back;
    TextView top_mune1,top_mune2;
    RelativeLayout rl_courtname;
    CheckBox ck_group,ck_person;//,ck_group2,ck_person2;

    EditText g_managersadminphone,g_managersadmin,g_managersname1,p_managersname1,socialUniformCreditCode;


    LinearLayout ll_charger,ll_admin,qqlogi,group1,person1;//,ll_group2,group2,group1,person1,person2;
    private static MyProInfo mInfo;
    private static int m_addtype;
    public static void start(Context context,MyProInfo info,int addtype) {
        Intent intent = new Intent(context, CreateProjectDetailTwoActivity.class);
        mInfo = info;
        m_addtype = addtype;
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        setContentView(R.layout.activity_create_pro_detail_two);

        initview();

        String user = SPUtils.getInstance(Consts.SPNAME).getString("loginUser");
        String pwd = SPUtils.getInstance(Consts.SPNAME).getString("loginPwd");

//        if(!user.equalsIgnoreCase(""))
//        {
//            et_username.setText(user);
//            et_password.setText(pwd);
//        }
        shapeLoadingDialog = new ShapeLoadingDialog(this);
        super.onCreate(savedInstanceState);
    }

    private int groupnum = 1;
    private int groupper = 1;
    private int groupper2 = 1;

    //初始化视图
    private void initview() {
        //登录层、下拉层、其它登录方式层


        et_username = findViewById(R.id.et_username);
        et_password = findViewById(R.id.et_password);
        top_back = findViewById(R.id.top_back);
        top_mune1 = findViewById(R.id.top_mune1);
        top_mune2 = findViewById(R.id.top_mune2);
        getPwd = findViewById(R.id.getPwd);
        linearLayout1 = findViewById(R.id.linearLayout1);
        l = new BtnLisener();
        cl= new CheckedChangeListener();
//        ll_group2 = findViewById(R.id.ll_group2);

        et_courtno = findViewById(R.id.et_courtno);
        et_courtname = findViewById(R.id.et_courtname);
        rl_courtname = findViewById(R.id.rl_courtname);
        rl_courtname.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(CreateProjectDetailTwoActivity.this, SearchCourtActivity.class);

                startActivityForResult(intent, REQUEST_SEL_COURT_ACTIVITY);
            }
        });




        im_groupadd = findViewById(R.id.im_groupadd);

//        im_groupadd2 = findViewById(R.id.im_groupadd2);
//        ll_group2.setVisibility(View.GONE);

        ck_person = findViewById(R.id.ck_person);
//        ck_person2 = findViewById(R.id.ck_person2);
        ck_group = findViewById(R.id.ck_group);
//        ck_group2 = findViewById(R.id.ck_group2);
        person1 = findViewById(R.id.person1);
//        person2 = findViewById(R.id.person2);
        group1 = findViewById(R.id.group1);
//        group2 = findViewById(R.id.group2);

        contacts= findViewById(R.id.contacts);
        contactsphone= findViewById(R.id.contactsphone);
        g_managersadminphone = findViewById(R.id.g_managersadminphone);
        g_managersadmin = findViewById(R.id.g_managersadmin);
        g_managersname1 = findViewById(R.id.g_managersname1);
        p_managersname1 = findViewById(R.id.p_managersname1);
        socialUniformCreditCode = findViewById(R.id.socialUniformCreditCode);


        if("3".equalsIgnoreCase(mInfo.getProjectType()))
        {
            findViewById(R.id.red_1).setVisibility(View.INVISIBLE);
            findViewById(R.id.red_2).setVisibility(View.INVISIBLE);
            findViewById(R.id.red_3).setVisibility(View.INVISIBLE);
            findViewById(R.id.red_4).setVisibility(View.INVISIBLE);
            findViewById(R.id.red_5).setVisibility(View.INVISIBLE);
            findViewById(R.id.red_6).setVisibility(View.INVISIBLE);
            findViewById(R.id.red_7).setVisibility(View.INVISIBLE);
            findViewById(R.id.red_8).setVisibility(View.INVISIBLE);
            findViewById(R.id.red_9).setVisibility(View.INVISIBLE);
            findViewById(R.id.red_10).setVisibility(View.INVISIBLE);

        }

        if("1".equalsIgnoreCase(mInfo.getAddRole()))
        {
            g_managersadminphone.setFocusable(false);

            g_managersadminphone.setFocusableInTouchMode(false);
            g_managersadmin.setText(BankruptcyApp.loginInfo.getUsername());

            g_managersadminphone.setText(BankruptcyApp.loginInfo.getLoginname());
        }else{
            g_managersadminphone.setFocusableInTouchMode(true);

            g_managersadminphone.setFocusable(true);

//            g_managersadmin.requestFocus();
        }
        if(m_addtype == 2)
        {
            top_mune2.setVisibility(View.INVISIBLE);
        }
        et_courtname.setText(mInfo.getCourtName());
        et_courtno.setText(mInfo.getCaseNumber());
//        person1.setVisibility(View.GONE);
//        person2.setVisibility(View.GONE);
        String  managerListStr= mInfo.getManagerListStr();
        if(!"".equalsIgnoreCase(managerListStr))
        {

            try{
                JSONArray ary  = new JSONArray(managerListStr);
                for(int i=0;i<ary.length();i++)
                {
                    JSONObject object = ary.getJSONObject(i);
                    String master = object.optString("master");
                    String managersType = object.optString("managersType");
                    if("true".equalsIgnoreCase(master))
                    {
                        if("0".equalsIgnoreCase(managersType))
                        {
                            ck_group.setChecked(true);
                            ck_person.setChecked(false);


                        }else{
//                            group1.setVisibility(View.GONE);
//                            person1.setVisibility(View.VISIBLE);
//                            ck_group.setChecked(false);
//                            ck_person.setChecked(true);
//                            p_managersname1.setText(object.optString("managersName"));
//                            contacts.setText(object.optString("contacts"));
//                            contactsphone.setText(object.optString("contactPhone"));
                        }
                    }else{
                        im_groupadd.setVisibility(View.VISIBLE);
//                        ll_group2.setVisibility(View.GONE);
                        if("0".equalsIgnoreCase(managersType))
                        {
//                            ck_group2.setChecked(true);
//                            ck_person2.setChecked(false);
//                            group2.setVisibility(View.VISIBLE);
//                            person2.setVisibility(View.GONE);
//                            g_managersname2.setText(object.optString("managersName"));
//                            g_managersadmin2.setText(object.optString("managersAdministrator"));
//                            g_managersadminphone2.setText(object.optString("managersAdministratorMobile"));
//                            socialUniformCreditCode2.setText(object.optString("socialUniformCreditCode"));
                        }else{
//                            ck_group2.setChecked(false);
//                            ck_person2.setChecked(true);
//                            group2.setVisibility(View.GONE);
//                            person2.setVisibility(View.VISIBLE);
//                            p_managersname2.setText(object.optString("managersName"));
//                            contacts2.setText(object.optString("contacts"));
//                            contactsphone2.setText(object.optString("contactPhone"));
                        }
                    }
                }
            }catch (Exception ex){
                ex.printStackTrace();
            }
        }


        ck_person.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    ck_group.setChecked(false);
                    groupper = 1;
                    person1.setVisibility(View.VISIBLE);
                    group1.setVisibility(View.GONE);
                }else{
                    ck_group.setChecked(true);
                }
            }
        });

        ck_group.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    ck_person.setChecked(false);
                    groupper = 0;
                    group1.setVisibility(View.VISIBLE);
                    person1.setVisibility(View.GONE);
                }else{
                    ck_person.setChecked(true);
                }
            }
        });



    }

    class BtnLisener implements OnClickListener{

        @Override
        public void onClick(View v) {
            switch (v.getId()) {

                case R.id.im_groupadd2:
                    View view = (View) v.getTag();
                    removeView(view);
                    break;

                default:
                    break;
            }

        }

    }

    class CheckedChangeListener implements CompoundButton.OnCheckedChangeListener {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            View view = (View) buttonView.getTag();
            CheckBox ck_group2 =  (CheckBox)view.findViewById(R.id.ck_group2);
            CheckBox ck_person2 = (CheckBox)view.findViewById(R.id.ck_person2);
            LinearLayout group2 = (LinearLayout)view.findViewById(R.id.group2);
            LinearLayout person2 = (LinearLayout)view.findViewById(R.id.person2);
            switch (buttonView.getId()) {

                case R.id.ck_group2:


                     if(ck_group2.isChecked())
                     {
                         ck_person2.setChecked(false);
                         group2.setVisibility(View.VISIBLE);
                         person2.setVisibility(View.GONE);
                     }else{
                         ck_person2.setChecked(true);
                         person2.setVisibility(View.VISIBLE);
                         group2.setVisibility(View.GONE);
                     }
                    break;
                case R.id.ck_person2:


                    if(ck_person2.isChecked())
                    {
                        ck_group2.setChecked(false);
                        person2.setVisibility(View.VISIBLE);
                        group2.setVisibility(View.GONE);
                    }else{
                        ck_group2.setChecked(true);
                        group2.setVisibility(View.VISIBLE);
                        person2.setVisibility(View.GONE);
                    }
                    break;

                default:
                    break;
            }


        }
    }

    private void removeView(View view){
        linearLayout1.removeView(view);
        editViews.remove(view);
        tmpnum --;
    }

    public final int REQUEST_SEL_COURT_ACTIVITY = 10001;
    String courtId = "";
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        switch (requestCode) {

            case REQUEST_SEL_COURT_ACTIVITY:
                if (resultCode == RESULT_OK) {
                    if (intent != null) {

                        String courtname = intent.getStringExtra("courtname");
                         courtId = intent.getStringExtra("courtid");
                        et_courtname.setText(courtname);

                    }
                }
                break;

        }
    }


    @Override
    public void onClick(View view) {

            String phone = "";
            String code = "";
            String pwd = "";
            switch (view.getId()) {
                case R.id.btn_login:
                    //返回
//
//                    LoginUser(phone, code, pwd);
                    break;
                case R.id.ll_phonelogin:
                    PhoneLoginActivity.start(this);
                    finish();
                    break;
                case R.id.top_back:
//                    finish();
                    dialogType =1;
                    noticeDialog = new PublicNoticeDialog(CreateProjectDetailTwoActivity.this,onClickListener,
                            1,"提示","暂存编辑？",
                            "否","是");
                    noticeDialog.show();
                    break;
                case R.id.top_mune2:
                    saveData(0);
                    break;
                case R.id.top_mune1:

                    String str_debtorName  = et_courtname.getText().toString();

                    if ("".equalsIgnoreCase(str_debtorName)&& !"3".equalsIgnoreCase(mInfo.getProjectType())) {
                        showToast("法院名称不能为空", com.linkage.seapeak.utils.ToastUtil.TOAST_TYPE.WARNING);
                        return;
                    }
                    mInfo.setCourtName(str_debtorName);
                    mInfo.setCourtId(courtId);
                    String str_CaseNumber  = et_courtno.getText().toString();
                    if ("".equalsIgnoreCase(str_CaseNumber)&& !"3".equalsIgnoreCase(mInfo.getProjectType())) {
                        showToast("法院案件编号不能为空", com.linkage.seapeak.utils.ToastUtil.TOAST_TYPE.WARNING);
//                        Toast.makeText(this, "法院案件编号不能为空", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    mInfo.setCaseNumber(str_CaseNumber);
                    contacts = findViewById(R.id.contacts);
                    contactsphone = findViewById(R.id.contactsphone);




                    JSONArray ary = new JSONArray();

                    try {

                        if (ck_group.isChecked()) {
                            String str_g_managersname1 = g_managersname1.getText().toString();
                            if ("".equalsIgnoreCase(str_g_managersname1)&& !"3".equalsIgnoreCase(mInfo.getProjectType())) {
//                                Toast.makeText(this, "管理人名称不能为空", Toast.LENGTH_SHORT).show();
                                showToast("管理人名称不能为空", com.linkage.seapeak.utils.ToastUtil.TOAST_TYPE.WARNING);
                                return;
                            }
                            String str_g_managersadmin = g_managersadmin.getText().toString();
                            if ("".equalsIgnoreCase(str_g_managersadmin)&& !"3".equalsIgnoreCase(mInfo.getProjectType())){
//                                Toast.makeText(this, "管理人负责人不能为空", Toast.LENGTH_SHORT).show();
                                showToast("管理人负责人不能为空", com.linkage.seapeak.utils.ToastUtil.TOAST_TYPE.WARNING);
                                return;
                            }
//
                            String str_g_managersadminphone = g_managersadminphone.getText().toString();
                            if ("".equalsIgnoreCase(str_g_managersadminphone)&& !"3".equalsIgnoreCase(mInfo.getProjectType())){
//                                Toast.makeText(this, "管理人负责人手机不能为空", Toast.LENGTH_SHORT).show();
                                showToast("管理人负责人手机不能为空", com.linkage.seapeak.utils.ToastUtil.TOAST_TYPE.WARNING);
                                return;
                            }
                            String str_socialUniformCreditCode = socialUniformCreditCode.getText().toString();
//                            if ("".equalsIgnoreCase(str_socialUniformCreditCode)&& !"3".equalsIgnoreCase(mInfo.getProjectType())){
//                                Toast.makeText(this, "社会信用代码不能为空", Toast.LENGTH_SHORT).show();
//                                showToast("管理人负责人手机不能为空", com.linkage.seapeak.utils.ToastUtil.TOAST_TYPE.WARNING);
//                                return;
//                            }

                            JSONObject obj = new JSONObject();
                            obj.put("managersType", "0");
                            obj.put("managersName", str_g_managersname1);
                            obj.put("managersAdministrator", str_g_managersadmin);
                            obj.put("managersAdministratorMobile", str_g_managersadminphone);
                            obj.put("socialUniformCreditCode", str_socialUniformCreditCode);
                            obj.put("master", "true");
                            ary.put(obj);
                        }

                        if (ck_person.isChecked()) {

                            String str_g_managersname1 = p_managersname1.getText().toString();
                            if ("".equalsIgnoreCase(str_g_managersname1)) {
//                                Toast.makeText(this, "管理人名称不能为空", Toast.LENGTH_SHORT).show();
                                showToast("管理人名称不能为空", com.linkage.seapeak.utils.ToastUtil.TOAST_TYPE.WARNING);
                                return;
                            }



                            JSONObject obj = new JSONObject();
                            obj.put("managersType", "1");
                            obj.put("managersName", str_g_managersname1);
                            obj.put("master", "true");
                            ary.put(obj);
                        }

                        for(int i=0;i<editViews.size();i++){
                            CheckBox ck_group2=(CheckBox)editViews.get(i).findViewById(R.id.ck_group2);
                            CheckBox ck_person2=(CheckBox)editViews.get(i).findViewById(R.id.ck_person2);

                            EditText g_managersname2 = (EditText)editViews.get(i).findViewById(R.id.g_managersname2);
                            EditText g_managersadmin2 = (EditText)editViews.get(i).findViewById(R.id.g_managersadmin2);
                            EditText g_managersadminphone2 = (EditText)editViews.get(i).findViewById(R.id.g_managersadminphone2);
                            EditText socialUniformCreditCode2 = (EditText)editViews.get(i).findViewById(R.id.socialUniformCreditCode2);
                            EditText p_managersname2 = (EditText)editViews.get(i).findViewById(R.id.p_managersname2);
                            TextView tv_grouptitle2 = (TextView)editViews.get(i).findViewById(R.id.tv_grouptitle2);



                            if (ck_group2.isChecked()) {
                                String str_g_managersname2 = g_managersname2.getText().toString();
                                if ("".equalsIgnoreCase(str_g_managersname2)) {
                                    String str  = tv_grouptitle2.getText().toString() +  "名称不能为空";
                                    showToast(str, com.linkage.seapeak.utils.ToastUtil.TOAST_TYPE.WARNING);
//                                    Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
                                    return;
                                }
                                String str_g_managersadmin2 = g_managersadmin2.getText().toString();
                                if ("".equalsIgnoreCase(str_g_managersadmin2)) {
                                    String str  = tv_grouptitle2.getText().toString() +  "负责人不能为空";
//                                    Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
                                    showToast(str, com.linkage.seapeak.utils.ToastUtil.TOAST_TYPE.WARNING);
                                    return;
                                }

                                String str_g_managersadminphone2 = g_managersadminphone2.getText().toString();
                                if ("".equalsIgnoreCase(str_g_managersadminphone2)) {
                                    String str  = tv_grouptitle2.getText().toString() +  "负责人手机不能为空";
//                                    Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
                                    showToast(str, com.linkage.seapeak.utils.ToastUtil.TOAST_TYPE.WARNING);
                                    return;
                                }
                                String str_socialUniformCreditCode2 = socialUniformCreditCode2.getText().toString();
//                                if ("".equalsIgnoreCase(str_socialUniformCreditCode2)) {
//                                    String str  = tv_grouptitle2.getText().toString() +  "社会信用代码不能为空";
//                                    Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
//                                    return;
//                                }

                                JSONObject obj = new JSONObject();
                                obj.put("managersType", "0");
                                obj.put("managersName", str_g_managersname2);
                                obj.put("managersAdministrator", str_g_managersadmin2);
                                obj.put("managersAdministratorMobile", str_g_managersadminphone2);
                                obj.put("socialUniformCreditCode", str_socialUniformCreditCode2);
                                obj.put("master", "false");
                                ary.put(obj);
                            }

                            if (ck_person2.isChecked()) {

                                String str_g_managersname2 = p_managersname2.getText().toString();
                                if ("".equalsIgnoreCase(str_g_managersname2)) {
                                    String str  = tv_grouptitle2.getText().toString() +  "名称不能为空";
//                                    Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
                                    showToast(str, com.linkage.seapeak.utils.ToastUtil.TOAST_TYPE.WARNING);
                                    return;
                                }

                                JSONObject obj = new JSONObject();
                                obj.put("managersType", "1");
                                obj.put("managersName", str_g_managersname2);
                                obj.put("master", "false");
                                ary.put(obj);
                            }
                        }

                    }catch (Exception ex)
                    {
                        ex.printStackTrace();
                    }

                    if ("".equalsIgnoreCase(contacts.getText().toString().trim())&& !"3".equalsIgnoreCase(mInfo.getProjectType())) {
                        showToast("联系人姓名不能为空", com.linkage.seapeak.utils.ToastUtil.TOAST_TYPE.WARNING);
//                        Toast.makeText(this, "联系人姓名不能为空", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    mInfo.setContacts(contacts.getText().toString().trim());

                    if ("".equalsIgnoreCase(contactsphone.getText().toString().trim())&& !"3".equalsIgnoreCase(mInfo.getProjectType())) {
                        showToast("联系电话不能为空", com.linkage.seapeak.utils.ToastUtil.TOAST_TYPE.WARNING);
//                        Toast.makeText(this, "联系人姓名不能为空", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    mInfo.setContactPhone(contactsphone.getText().toString().trim());

                    String json = ary.toString();
                    mInfo.setManagerListStr(json);

                    CreateProjectDetailThreeActivity.start(this,mInfo,m_addtype);
                    finish();
                    break;
                case R.id.im_groupadd:

//                    groupnum = 2;
//                    im_groupadd.setVisibility(View.INVISIBLE);
//                    ll_group2.setVisibility(View.VISIBLE);
                    addView();
                    break;
                case R.id.im_groupadd2:
                    groupnum = 1;
                    im_groupadd.setVisibility(View.VISIBLE);
//                    ll_group2.setVisibility(View.GONE);
                    break;
                default:
                    break;
            }


    }
    private OnClickListener l;
    private CheckedChangeListener cl;
    private LinearLayout linearLayout1;
    private void addView(){
        View view = LayoutInflater.from(getBaseContext()).inflate(R.layout.activity_create_pro_detail_two_item, null);
        ImageView btnDel = (ImageView) view.findViewById(R.id.im_groupadd2);
        CheckBox ck_group2=(CheckBox)view.findViewById(R.id.ck_group2);
        CheckBox ck_person2=(CheckBox)view.findViewById(R.id.ck_person2);

        if("3".equalsIgnoreCase(mInfo.getProjectType())) {
            view.findViewById(R.id.red10_1).setVisibility(View.INVISIBLE);
            view.findViewById(R.id.red10_2).setVisibility(View.INVISIBLE);
            view.findViewById(R.id.red10_3).setVisibility(View.INVISIBLE);
            view.findViewById(R.id.red10_4).setVisibility(View.INVISIBLE);
            view.findViewById(R.id.red10_5).setVisibility(View.INVISIBLE);
            view.findViewById(R.id.red10_6).setVisibility(View.INVISIBLE);
        }else{
            view.findViewById(R.id.red10_1).setVisibility(View.VISIBLE);
            view.findViewById(R.id.red10_2).setVisibility(View.VISIBLE);
            view.findViewById(R.id.red10_3).setVisibility(View.VISIBLE);
            view.findViewById(R.id.red10_4).setVisibility(View.VISIBLE);
            view.findViewById(R.id.red10_5).setVisibility(View.VISIBLE);
            view.findViewById(R.id.red10_6).setVisibility(View.VISIBLE);
        }


        EditText g_managersname2 = (EditText)view.findViewById(R.id.g_managersname2);
        EditText g_managersadmin2 = (EditText)view.findViewById(R.id.g_managersadmin2);
        EditText g_managersadminphone2 = (EditText)view.findViewById(R.id.g_managersadminphone2);
        EditText socialUniformCreditCode2 = (EditText)view.findViewById(R.id.socialUniformCreditCode2);

        EditText p_managersname2 = (EditText)view.findViewById(R.id.p_managersname2);


        TextView tv_grouptitle2 = (TextView)view.findViewById(R.id.tv_grouptitle2);
        if(tmpnum == 1)
        {
            num =1;
        }
        num++;
        tmpnum++;
        tv_grouptitle2.setText("管理人");

        btnDel.setTag(view);
        btnDel.setOnClickListener(l);
        ck_group2.setOnCheckedChangeListener(cl);
        ck_group2.setTag(view);
        ck_person2.setOnCheckedChangeListener(cl);
        ck_person2.setTag(view);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        linearLayout1.addView(view, params);

        editViews.add(view);
    }

    public void saveData(int type)
    {

        if(BankruptcyApp.tokeninfo == null|| BankruptcyApp.tokeninfo.getToken() == null)
        {
            getToken();
            return;
        }

        OkHttpClient okHttpClient = new OkHttpClient();
        HashMap<String, String> params = new HashMap<String, String>();
        Random random = new Random();
        String rom = random.nextLong()+"";
        String timestamp =System.currentTimeMillis()+"";

        String str_debtorName  = et_courtname.getText().toString();
//        if ("".equalsIgnoreCase(str_debtorName)&& !"3".equalsIgnoreCase(mInfo.getProjectType())) {
//            Toast.makeText(this, "法院名称不能为空", Toast.LENGTH_SHORT).show();
//            return;
//        }
        mInfo.setCourtName(str_debtorName);
        mInfo.setCourtId(courtId);
        String str_CaseNumber  = et_courtno.getText().toString();
//        if ("".equalsIgnoreCase(str_CaseNumber)&& !"3".equalsIgnoreCase(mInfo.getProjectType())) {
//            Toast.makeText(this, "法院案件编号不能为空", Toast.LENGTH_SHORT).show();
//            return;
//        }
        mInfo.setCaseNumber(str_CaseNumber);

//        if (groupper ==-1&& !"3".equalsIgnoreCase(mInfo.getProjectType())) {
//            Toast.makeText(this, "请选择管理人类型", Toast.LENGTH_SHORT).show();
//            return;
//        }
//        mInfo.setCaseNumber(str_projectName);

        contacts = findViewById(R.id.contacts);
        contactsphone = findViewById(R.id.contactsphone);


//        if ("".equalsIgnoreCase(contacts.getText().toString().trim())&& !"3".equalsIgnoreCase(mInfo.getProjectType())) {
//            Toast.makeText(this, "联系人姓名不能为空", Toast.LENGTH_SHORT).show();
//            return;
//        }
        mInfo.setContacts(contacts.getText().toString().trim());

//        if ("".equalsIgnoreCase(contactsphone.getText().toString().trim())&& !"3".equalsIgnoreCase(mInfo.getProjectType())) {
//            Toast.makeText(this, "联系人姓名不能为空", Toast.LENGTH_SHORT).show();
//            return;
//        }
        mInfo.setContactPhone(contactsphone.getText().toString().trim());

        JSONArray ary = new JSONArray();

        try {

            if (ck_group.isChecked()) {
                String str_g_managersname1 = g_managersname1.getText().toString();
//                if ("".equalsIgnoreCase(str_g_managersname1)) {
//                    Toast.makeText(this, "管理人名称不能为空", Toast.LENGTH_SHORT).show();
//                    return;
//                }
                String str_g_managersadmin = g_managersadmin.getText().toString();
//                if ("".equalsIgnoreCase(str_g_managersadmin)) {
//                    Toast.makeText(this, "管理人负责人不能为空", Toast.LENGTH_SHORT).show();
//                    return;
//                }

                String str_g_managersadminphone = g_managersadminphone.getText().toString();
//                if ("".equalsIgnoreCase(str_g_managersadminphone)) {
//                    Toast.makeText(this, "管理人负责人手机不能为空", Toast.LENGTH_SHORT).show();
//                    return;
//                }
                String str_socialUniformCreditCode = socialUniformCreditCode.getText().toString();
//                if ("".equalsIgnoreCase(str_socialUniformCreditCode)) {
//                    Toast.makeText(this, "社会信用代码不能为空", Toast.LENGTH_SHORT).show();
//                    return;
//                }

                JSONObject obj = new JSONObject();
                obj.put("managersType", "0");
                obj.put("managersName", str_g_managersname1);
                obj.put("managersAdministrator", str_g_managersadmin);
                obj.put("managersAdministratorMobile", str_g_managersadminphone);
                obj.put("socialUniformCreditCode", str_socialUniformCreditCode);
                obj.put("master", "true");
                ary.put(obj);
            }

            if (ck_person.isChecked()) {

                String str_g_managersname1 = p_managersname1.getText().toString();
//                if ("".equalsIgnoreCase(str_g_managersname1)) {
//                    Toast.makeText(this, "管理人名称不能为空", Toast.LENGTH_SHORT).show();
//                    return;
//                }



                JSONObject obj = new JSONObject();
                obj.put("managersType", "1");
                obj.put("managersName", str_g_managersname1);
                obj.put("master", "true");
                ary.put(obj);
            }

            for(int i=0;i<editViews.size();i++){
                CheckBox ck_group2=(CheckBox)editViews.get(i).findViewById(R.id.ck_group2);
                CheckBox ck_person2=(CheckBox)editViews.get(i).findViewById(R.id.ck_person2);

                EditText g_managersname2 = (EditText)editViews.get(i).findViewById(R.id.g_managersname2);
                EditText g_managersadmin2 = (EditText)editViews.get(i).findViewById(R.id.g_managersadmin2);
                EditText g_managersadminphone2 = (EditText)editViews.get(i).findViewById(R.id.g_managersadminphone2);
                EditText socialUniformCreditCode2 = (EditText)editViews.get(i).findViewById(R.id.socialUniformCreditCode2);
                EditText p_managersname2 = (EditText)editViews.get(i).findViewById(R.id.p_managersname2);
                TextView tv_grouptitle2 = (TextView)editViews.get(i).findViewById(R.id.tv_grouptitle2);



                if (ck_group2.isChecked()) {
                    String str_g_managersname2 = g_managersname2.getText().toString();
//                    if ("".equalsIgnoreCase(str_g_managersname2)) {
//                        String str  = tv_grouptitle2.getText().toString() +  "名称不能为空";
//                        Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
//                        return;
//                    }
                    String str_g_managersadmin2 = g_managersadmin2.getText().toString();
//                    if ("".equalsIgnoreCase(str_g_managersadmin2)) {
//                        String str  = tv_grouptitle2.getText().toString() +  "负责人不能为空";
//                        Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
//                        return;
//                    }

                    String str_g_managersadminphone2 = g_managersadminphone2.getText().toString();
//                    if ("".equalsIgnoreCase(str_g_managersadminphone2)) {
//                        String str  = tv_grouptitle2.getText().toString() +  "负责人手机不能为空";
//                        Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
//                        return;
//                    }
                    String str_socialUniformCreditCode2 = socialUniformCreditCode2.getText().toString();
//                    if ("".equalsIgnoreCase(str_socialUniformCreditCode2)) {
//                        String str  = tv_grouptitle2.getText().toString() +  "社会信用代码不能为空";
//                        Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
//                        return;
//                    }

                    JSONObject obj = new JSONObject();
                    obj.put("managersType", "0");
                    obj.put("managersName", str_g_managersname2);
                    obj.put("managersAdministrator", str_g_managersadmin2);
                    obj.put("managersAdministratorMobile", str_g_managersadminphone2);
                    obj.put("socialUniformCreditCode", str_socialUniformCreditCode2);
                    obj.put("master", "false");
                    ary.put(obj);
                }

                if (ck_person2.isChecked()) {

                    String str_g_managersname2 = p_managersname2.getText().toString();
                    if ("".equalsIgnoreCase(str_g_managersname2)) {
                        String str  = tv_grouptitle2.getText().toString() +  "名称不能为空";
                        Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
                        return;
                    }

                    JSONObject obj = new JSONObject();
                    obj.put("managersType", "1");
                    obj.put("managersName", str_g_managersname2);
                    obj.put("master", "false");
                    ary.put(obj);
                }
            }

        }catch (Exception ex)
        {
            ex.printStackTrace();
        }


        shapeLoadingDialog.setLoadingText("提交信息中...");
        shapeLoadingDialog.setCanceledOnTouchOutside(false);
        shapeLoadingDialog.show();
        mInfo.setProjectStatus("0");
        mInfo.setPublicSection("0,1");

        String json = ary.toString();
        mInfo.setManagerListStr(json);

        String sign = SignUtil.createSign(Consts.APPKEY,params);
        String userToken = SPUtils.getInstance(Consts.SPNAME).getString("userToken","");

        RequestBody requestBody = new FormBody.Builder()
                .add("appid","1000000001")
                .add("nonce",rom)
                .add("timestamp", timestamp)
                .add("token", BankruptcyApp.tokeninfo.getToken())
                .add("userToken", userToken)
//                .add("appVersion", "0.0.1")

                .add("addRole", mInfo.getAddRole())
                .add("id", mInfo.getId())
                .add("debtorName", mInfo.getDebtorName())
                .add("projectStatus",  "0")
                .add("projectType",  mInfo.getProjectType())
                .add("projectName",  mInfo.getProjectName())
                .add("projectLogo",  mInfo.getProjectLogo())
                .add("projectNote",  mInfo.getProjectNote())
                .add("courtName",  mInfo.getCourtName())
                .add("courtId",  mInfo.getCourtId())
                .add("caseNumber",  mInfo.getCaseNumber())
                .add("debtorLegalRepresentative",  mInfo.getDebtorLegalRepresentative())
                .add("sysAddressId",  mInfo.getSysAddressId())
                .add("publicStatus", "1")
                .add("publicSection",  mInfo.getPublicSection())
                .add("close",  mInfo.getClose())
                .add("allowJoin",  mInfo.getAllowJoin())
                .add("closeClaimFiling",  mInfo.getCloseClaimFiling())
                .add("openDiscussion",  mInfo.getOpenDiscussion())
                .add("allowCreditorsChangInfo",  mInfo.getAllowCreditorsChangInfo())
                .add("displayedInstantly",  mInfo.getDisplayedInstantly())
                .add("creditorsVisibleDetails",  mInfo.getCreditorsVisibleDetails())
                .add("openAdministratorList",  mInfo.getOpenAdministratorList())

                .add("openAdministratorInfo",  mInfo.getOpenAdministratorInfo())
                .add("openCreditorsList",  mInfo.getOpenCreditorsList())
                .add("openCreditorsInfo",  mInfo.getOpenCreditorsInfo())

                .add("managerListStr",  mInfo.getManagerListStr())
                .add("debtorYyzzFileListStr",  mInfo.getDebtorYyzzFileListStr())
                .add("courtSlwsFileListStr",  mInfo.getCourtSlwsFileListStr())
                .add("courtManagerFileListStr",  mInfo.getCourtManagerFileListStr())
                .add("otherFileListStr",  mInfo.getOtherFileListStr())
                .add("contacts", mInfo.getContacts())
                .add("contactPhone", mInfo.getContactPhone())
                .add("courtId",  mInfo.getCourtId())


                .build();

        String url = Consts.WEBURL_ADDPRO;
        if(!"".equalsIgnoreCase(mInfo.getId()))
        {
            url = Consts.WEBURL_UPDATEPRO;
        }
        Request request = new Request.Builder()
                .url(url)
                .post(requestBody)
                .build();

        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
//                Log.d(TAG, "onFailure: " + e.getMessage());
                shapeLoadingDialog.dismiss();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
//                Log.d(TAG, response.protocol() + " " +response.code() + " " + response.message());
                Headers headers = response.headers();


                shapeLoadingDialog.dismiss();
                String str = response.body().string();
                try{
                    JSONObject object = new JSONObject(str);
                    if(object.optInt("code") == 0)
                    {

                        runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                EventBus.getDefault().post(mInfo);
                                //更新UI
                                Toast.makeText(CreateProjectDetailTwoActivity.this,
                                        "提交成功",Toast.LENGTH_SHORT).show();
                                finish();

                            }

                        });
                    }else{
                        runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                //更新UI
                                Toast.makeText(CreateProjectDetailTwoActivity.this,
                                        "提交失败",Toast.LENGTH_SHORT).show();


                            }

                        });
                    }

                    str = "";
                }catch (Exception ex)
                {

                    ex.printStackTrace();
                }
            }
        });


    }
    private  static int dialogType = 1;
    private PublicNoticeDialog noticeDialog;
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {


        if (keyCode == KeyEvent.KEYCODE_BACK) {
//            exit();
//            return false;
            dialogType =1;
            noticeDialog = new PublicNoticeDialog(CreateProjectDetailTwoActivity.this,onClickListener,
                    1,"提示","暂存编辑？",
                    "否","是");
            noticeDialog.show();
        }
        return super.onKeyDown(keyCode, event);
    }

    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            switch (v.getId()) {
                case R.id.ll_close:
                    noticeDialog.dismiss();
                    break;

                case R.id.btn_chancel_notice:
//                   Boolean flag =  noticelxDialog.cb_agree.isChecked();
                    if(dialogType ==1 ) {
                        finish();
                    }else{

                    }
                    noticeDialog.dismiss();

                    break;

                case R.id.btn_save_notice:
                    if(dialogType== 1) {
                        saveData(1);
                    }
                    noticeDialog.dismiss();
                    break;

            }
        }
    };

}

